<script>
import settings, { getEnv } from "./utils/settings"
import store from "./store"
import { userActionType } from "./store/modules/user"
import { handleUrlParams, valIsEmpty } from "@/utils"
import websocket from "@/utils/websocket"
import urlUtils from "@/utils/urlUtils"
import persistence from "@/store/persistence"
export default {
  onShow: function (options) {
    const query = options.query ? options.query : {}
    //渠道码
    if (!valIsEmpty(query.channelId)) {
      console.log(query.channelId, "带进来的渠道码")
      store.commit(userActionType.mutations.channelId, query.channelId)
    }
    //保存推荐官id
    //卡片分享
    let shareUserId, shareType
    if (query.shareUserId) {
      console.log(query.shareUserId, "shareUserId")
      shareUserId = query.shareUserId
      shareType = query.shareType
    }
    //二维码分享
    if (query.scene && query.scene.startsWith("TJG")) {
      shareUserId = query.scene.substring(3)
      shareType = query.scene.substring(0, 3)
    }
    if (shareUserId) {
      // store.commit(userActionType.mutations.recommender.id, shareUserId)
      persistence.inviteUserId.set(shareUserId)
			persistence.shareUserId.set(shareUserId)
    }
    if (shareType) {
      persistence.shareType.set(shareType)
    }

    //检查小程序是否有新版
    if (uni && uni.getUpdateManager) {
      const updateManager = uni.getUpdateManager()
      // console.log('检测小程序是否需要更新')
      // 在分享朋友圈单页模式中,暂时没有getUpdateManager,返回值为undefined
      if (updateManager) {
        updateManager.onCheckForUpdate((result) => {
          if (result.hasUpdate) {
            updateManager.onUpdateReady(() => {
              uni.showModal({
                title: "更新提示",
                content: "新版本已经上线啦~",
                confirmText: "立即体验",
                showCancel: false,
                success: (result) => {
                  updateManager.applyUpdate()
                },
              })
            })
            updateManager.onUpdateFailed(() => {
              uni.showModal({
                title: "有新版本了哟",
                content: "新版本已经上线啦，重启微信再进入体验~",
                showCancel: false,
              })
            })
          }
        })
      }
    }
  },
  onHide: function () {},
  async onLaunch(options) {
		if (options && options.query) {
			// 保存用户来源参数
			if (options.query.userFrom) {
				store.commit(userActionType.mutations.userFrom, options.query.userFrom)
			}
			// 保存校务行的用户来源学校
			if (options.query.XWX_schoolName_7p4t) {
				store.commit(userActionType.mutations.XWX_schoolName, options.query.XWX_schoolName_7p4t)
			}
		}

    // for debug
    // if (["develop"].includes(getEnv())) {
      // const url = "markingCampaign/pages/receiveCouponByCouponId/index.html?appmsg_compact_url=https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FQoKZnM9Xo8cNtQlRhdu5MA&redirectUrl=%2Fpages%2Fcourse%2Fdetail%3Fid%3D23&posterUrl=https%3A%2F%2Fkou.gdzmtech.com%2Fpic%2F4567f2d26aef.png&appid=wxc55f46a2c1a270c9&couponId=1808060393080913922"
			// const obj = urlUtils.parseUrl(url)
			// console.log(decodeURIComponent(obj.query.posterUrl), "obj.query.posterUrl")
    // }

    //初始化websocket连接
    websocket.init().catch(() => {})
  },
}
</script>

<style lang="scss">
/*每个页面公共css */
@import "./static/style/global";
</style>
